package api

import (
	"OJCenter/global"
	"OJCenter/idl"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
	"net/http"
)

// @Summary	添加公告
// @Produce	json
// @Param		body	body		idl.Notice		true	"公告信息"
// @Success	200		{object}	global.Response	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/createNotice [post]
func (a *Api) CreateNotice(c *gin.Context) {
	var Notice idl.Notice

	err := c.ShouldBindJSON(&Notice)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	err = validate.Struct(&Notice)
	if err != nil {
		global.MarkErrors(err.(validator.ValidationErrors))
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}

	Uuid, _ := uuid.NewRandom()
	Notice.Nid = int64(Uuid.ID())

	err = a.M.CreateNotice(&Notice)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	删除公告
// @Produce	json
// @Param		body	body		[]int			true	"公告ID"
// @Success	200		{object}	global.Response	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/deleteNotice [post]
func (a *Api) DeleteNotice(c *gin.Context) {

	var nids []int64
	err := c.ShouldBindJSON(&nids)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	sum, fu := a.M.DeleteContestByCID(nids)
	if sum != len(nids) && fu != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, fu)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	更新公告
// @Produce	json
// @Param		body	body		[]idl.Notice	true	"公告信息"
// @Success	200		{object}	global.Response	""
// @Fail		400  {object} global.Response{Data=[]idl.Notice} ""
// @Router		/api/v1/updateNotice [post]
func (a *Api) UpdateNotice(c *gin.Context) {
	var Notice []idl.Notice

	err := c.ShouldBindJSON(&Notice)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	for _, t := range Notice {
		err := validate.Struct(&t)
		if err != nil {
			global.MarkErrors(err.(validator.ValidationErrors))
			global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
			return
		}
	}

	sum, fu := a.M.UpdateNotices(&Notice)
	if sum != len(Notice) {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, fu)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}

// @Summary	获取通告列表
// @Produce	json
// @Param		page	query		int									true	"页码"
// @Param		limit	query		int									true	"限制"
// @Success	200		{object}	global.Response{Data=idl.Notice}	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/getNoticeList [Get]
func (a *Api) GetNoticeList(c *gin.Context) {
	var Notice []idl.Notice

	err := c.ShouldBindJSON(&Notice)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	for _, t := range Notice {
		err := validate.Struct(&t)
		if err != nil {
			global.MarkErrors(err.(validator.ValidationErrors))
			global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
			return
		}
	}

	sum, fu := a.M.SelectNoticesByCID(&Notice)
	if sum != len(Notice) {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, fu)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, new(interface{}))
}
